﻿@model IContentQuery<Kooboo.CMS.Content.Models.TextContent>
@{
    Layout = "~/Areas/" + Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.FoodOrderingAreaRegistration.ModuleName + "/Views/Shared/Front.cshtml";
    int dayNum = (int)ViewBag.DayOfWeek;
 }
@Html.ModuleHtml().Partial("MealTime")
<div class="top-nav-items">
    <ul>
        <li @if (dayNum == 0)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 0 })" id="0">@("Sun".Label())</a>
        </li>
        <li @if (dayNum == 1)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 1 })" id="1">@("Mon".Label())</a>
        </li>
        <li @if (dayNum == 2)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 2 })" id="2">@("Tue".Label())</a>
        </li>
        <li @if (dayNum == 3)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 3 })" id="3">@("Wed".Label())</a>
        </li>
        <li @if (dayNum == 4)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 4 })" id="4">@("Thur".Label())</a>
        </li>
        <li @if (dayNum == 5)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 5 })" id="5">@("Fri".Label())</a>
        </li>
        <li @if (dayNum == 6)
            {@("class=on")}><a href="@Url.ModuleUrl().Action("Index", new { dayOfWeek = 6 })" id="6">@("Sat".Label())</a>
        </li>
    </ul>
</div>
@foreach (var item in Model)
{	

    <div class="block" @ViewHelper.Edit(item)>
        <h4>
            <span class="title">@item["Name"]</span><span class="comment">@item["Comment"]</span></h4>
        <div class="table">
            <ul id='@("currentlist" + item["Id"])'>
                @foreach (var food in item.Children(Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.FoodOrderingAreaRegistration.FoodFolderName).WhereEquals("Published", true))
                {
                    if (food["MealTime"].ToString().IndexOf("-1") > -1 || food["MealTime"].ToString().IndexOf(dayNum.ToString()) > -1)
                    {
                    <li><a href="#" class="btnOrder" data-uuid="@food["UUID"]">@food["Name"]
                    </a><span>￥@Decimal.Parse(@food["Price"].ToString()).ToString("f1")</span> </li>    
                    }
                }</ul>
        </div>
    </div>
}
@section sidebar{
    @{
        var allList = ContentHelper.TextFolder(Kooboo.CMS.FoodOrdering.Areas.FoodOrdering.FoodOrderingAreaRegistration.OrderDetailFolderName)
            .CreateQuery().WhereGreaterThan("OrderTime", DateTime.Now.AddMonths(-1).ToString("yyyyMMdd")).WhereEquals("Published", true);
        IEnumerable<Kooboo.CMS.Content.Models.TextContent> yourList = null;
        if (Kooboo.CMS.FoodOrdering.Areas.Customer.Models.LoginModel.Current.IsAuthenticated)
        {
            yourList = allList
             .WhereEquals("User", Kooboo.CMS.FoodOrdering.Areas.Customer.Models.LoginModel.Current.Username);
        }
        ViewDataDictionary viewData = new ViewDataDictionary();
    }
    @if (yourList != null)
    {
        viewData["Title"] = "Your Top 5".Label();
        @Html.ModuleHtml().Partial("TopView", yourList, viewData) 
    }
    @{ viewData["Title"] = "All Top 5".Label(); }
    @Html.ModuleHtml().Partial("TopView", allList, viewData)
}
@if (Kooboo.CMS.FoodOrdering.Areas.Customer.Models.LoginModel.Current.IsAuthenticated)
{ 
    <script language="javascript" type="text/javascript">
        $(function () {
            function orderSubmit() {
                var el = $(this);
                var uuid = el.attr("data-uuid");
                var meal = $("input:radio[name=form_mealtime]:checked").val();
                $.ajax({
                    type: "POST",
                    url: '@Url.ModuleUrl().Action("Order")',
                    data: { uuid: uuid, meal: meal },
                    dataType: "json",
                    beforeSend: function () {
                        $(".loading").show();
                    },
                    success: function (json) {
                        if (json == null) return;
                        if (json.Success == false) {
                            alert(json.Messages[0]);
                        }
                        else {
                            var restname = json.Model.rest;
                            var foodname = json.Model.food;

                            if (json.Model.meal == "1") {
                                $("#dvUserPanelLunch").show();
                                $("#dvUserPanelLunch").find("span").text("[" + restname + "]" + " " + foodname + " ￥" + json.Model.price);
                                $("#dvUserPanelLunch").find("a").text(json.Model.uuid);
                                $("#dvUserPanelLunch").find("a").attr("meal", 1);
                            } else if (json.Model.meal == "2") {
                                $("#dvUserPanelSupper").show();
                                $("#dvUserPanelSupper").find("span").text("[" + restname + "]" + " " + foodname + " ￥" + json.Model.price);
                                $("#dvUserPanelSupper").find("a").text(json.Model.uuid);
                                $("#dvUserPanelLunch").find("a").attr("meal", 2);
                            }
                        }
                    },
                    complete: function () {
                        $(".loading").hide();
                    }
                });
            }

            function cancelSumbit() {
                var el = $(this);
                var uuid = el.text();
                var meal = el.attr("meal");
                $.ajax({
                    type: "POST",
                    url: '@Url.ModuleUrl().Action("Delete")',
                    data: { uuid: uuid },
                    dataType: "json",
                    beforeSend: function () {
                        $(".loading").show();
                    },
                    success: function (json) {
                        if (window.top.self.location.href.toLowerCase().indexOf("/records") > 0) {
                            reloadPage();
                        }
                        else {
                            if (json == null) return;
                            if (json.Success == false) {
                                alert(json.Messages[0]);
                            }
                            else {
                                if (meal == "1") {
                                    $("#dvUserPanelLunch").find("span").text("");
                                    $("#dvUserPanelLunch").find("a").text("");
                                    $("#dvUserPanelLunch").hide();
                                } else if (meal == "2") {
                                    $("#dvUserPanelSupper").find("span").text("");
                                    $("#dvUserPanelSupper").find("a").text("");
                                    $("#dvUserPanelSupper").hide();
                                }
                            }
                        }
                    },
                    complete: function () {
                        $(".loading").hide();
                    }
                });
            }

            $('.btnOrder').click(orderSubmit);
            $('.delete').click(cancelSumbit);
        });
    </script>
}
